/**
 * 
 */
package fr.isima.isipaint.model;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.util.Iterator;
import java.util.LinkedList;

import javax.swing.SwingUtilities;

/**
 * @author daniel
 * 
 */
public final class GraphicGroup extends GraphicObject {

	/** The list of graphic objects */
	LinkedList<GraphicObject> group;

	/**
	 * @return the group
	 */
	public final LinkedList<GraphicObject> getGroup() {
		return group;
	}

	/**
	 * The default constructor
	 */
	public GraphicGroup() {
		super();
		group = new LinkedList<GraphicObject>();
	}

	/**
	 * To add an object into the list
	 * 
	 * @param object
	 *            The object to add
	 */
	public void addGraphicObject(GraphicObject object) {
		group.addFirst(object);
		add(object, 0);
	}

	/*
	 * public void addGraphicObject(GraphicGroup group) { for(GraphicObject
	 * object : group.getGroup()) {
	 * object.setLocation(SwingUtilities.convertPoint(group,
	 * object.getLocation(), this)); this.group.addFirst(object); add(object,0);
	 * } }
	 */
	/**
	 * 
	 * @param object
	 *            The object to remove
	 * @return The success code
	 */
	public boolean removeGraphicObject(GraphicObject object) {
		remove(object);
		return group.remove(object);
	}

	/**
	 * Remove all Graphic object from this group
	 */
	public void removeAllGraphicObject() {
		for (GraphicObject object : group) {
			remove(object);
		}

		group.clear();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
	 */
	@Override
	protected void paintComponent(Graphics g) {
		Graphics2D g2 = (Graphics2D) g;
		super.paintComponent(g2);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#clone()
	 */
	@Override
	public Object clone() {
		Scene object = null;

		object = (Scene) super.clone();

		for (GraphicObject it : group) {
			object.addGraphicObject(it);
		}

		return object;
	}

	/**
	 * 
	 * @param x
	 *            The x coordinate
	 * @param y
	 *            The y coordinate
	 * @return The object (in foreground) located in this point
	 */
	public GraphicObject getGraphicObject(int x, int y) {

		GraphicObject res = null;

		// Iterate from the end of the list because we need the top layer
		for (Iterator<GraphicObject> it = group.descendingIterator(); it
				.hasNext();) {
			GraphicObject tmp = it.next();
			if (tmp.contains(x, y)) {
				res = tmp;
			}
		}
		return res;
	}

	/**
	 * 
	 * @param bounds
	 *            The selection
	 * @return The group of graphic objects contained in this selection
	 */
	public GraphicGroup getBox(Rectangle bounds) {

		GraphicGroup box = new GraphicGroup();
		System.out.println(bounds);
		int xLeft = bounds.width - bounds.x;
		int yTop = bounds.height - bounds.y;

		int xRight = -1;
		int yBottom = -1;

		for (GraphicObject object : group) {
			if (bounds.contains(object.getBounds())) {
				GraphicObject copy = (GraphicObject) object;
				box.addGraphicObject(copy);				
				if (copy.getX() < xLeft) {
					xLeft = copy.getX();
				}
				if (copy.getX() + copy.getWidth() > xRight) {
					xRight = copy.getX() + copy.getWidth();
				}
				if (copy.getY() < yTop) {
					yTop = copy.getY();
				}
				if (copy.getY() + copy.getHeight() > yBottom) {
					yBottom = copy.getY() + copy.getHeight();
				}
			}
		}

		box.setBounds(xLeft - 1, yTop - 1, xRight - xLeft + 2, yBottom - yTop
				+ 2);
		System.out.println(box.getBounds());
		for (GraphicObject object : box.getGroup()) {			
			object.setLocation(object.getX() - xLeft + 1, object.getY() - yTop
					+ 1);
		}
		if (xLeft == -1 && xRight == -1 && yTop == -1 && yBottom == -1) {
			box = null;
		}

		return box;
	}

}
